package org.backend.canteenbackend.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.backend.canteenbackend.common.CommonResponse;
import org.backend.canteenbackend.entity.Account;
import org.backend.canteenbackend.entity.AccountSignOn;
import org.backend.canteenbackend.entity.Profile;
import org.backend.canteenbackend.persistence.AccountMapper;
import org.backend.canteenbackend.persistence.AccountSignOnMapper;
import org.backend.canteenbackend.persistence.ProfileMapper;
import org.backend.canteenbackend.service.AccountService;
import org.backend.canteenbackend.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private AccountSignOnMapper accountSignOnMapper;
    @Autowired
    private ProfileMapper profileMapper;

    public CommonResponse<Account> login(String username, String password) {
        QueryWrapper<AccountSignOn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        AccountSignOn accountSignOn = accountSignOnMapper.selectOne(queryWrapper);
        if (accountSignOn == null) {
            return CommonResponse.createForError("用户名或密码错误");
        }else {
            Account account = accountMapper.selectById(username);
            return CommonResponse.createForSuccess("登录成功",account);
        }
    }

    public CommonResponse<Object> register(AccountVO accountVO) {
        if(accountMapper.selectById(accountVO.getUsername())!=null){
            return CommonResponse.createForError("用户名已被注册");
        }
        Account account = accountVOToAccount(accountVO);
        AccountSignOn accountSignOn = accountVOToAccountSignOn(accountVO);
        Profile profile=accountVOToProfile(accountVO);
        int result1 = accountMapper.insert(account);
        int result2 = accountSignOnMapper.insert(accountSignOn);
        int result3 = profileMapper.insert(profile);
        if (result1 != 0 && result2 != 0 && result3 != 0) {
            return CommonResponse.createForSuccess("用户注册成功");
        }
        else {
            return CommonResponse.createForError("服务器异常");
        }
    }

    @Override
    public CommonResponse<AccountVO> viewInformation(String username) {
        Account account=accountMapper.selectById(username);
        AccountVO accountVO=accountToAccountVO(account);
        return CommonResponse.createForSuccess(accountVO);
    }

    @Override
    public CommonResponse<Object> editAccount(AccountVO accountVO) {
        int result1=accountMapper.updateById(accountVOToAccount(accountVO));
        int result2=accountSignOnMapper.updateById(accountVOToAccountSignOn(accountVO));
        int result3=profileMapper.updateById(accountVOToProfile(accountVO));

        if (result1 != 0 && result2 != 0 && result3 != 0) {
            return CommonResponse.createForSuccess("用户信息修改成功");
        }
        else {
            return CommonResponse.createForError("服务器异常");
        }
    }

    private AccountVO accountToAccountVO(Account account) {
        AccountVO accountVO = new AccountVO();
        accountVO.setUsername(account.getUsername());
        accountVO.setEmail(account.getEmail());
        accountVO.setFirstName(account.getFirstName());
        accountVO.setLastName(account.getLastName());
        accountVO.setStatus(account.getStatus());
        accountVO.setAddress1(account.getAddress1());
        accountVO.setAddress2(account.getAddress2());
        accountVO.setCity(account.getCity());
        accountVO.setState(account.getState());
        accountVO.setZip(account.getZip());
        accountVO.setCountry(account.getCountry());
        accountVO.setPhone(account.getPhone());
        AccountSignOn accountSignOn = accountSignOnMapper.selectById(account.getUsername());
        if(accountSignOn!=null)accountVO.setPassword(accountSignOn.getPassword());
        Profile profile= profileMapper.selectById(account.getUsername());
        if(profile!=null){
            accountVO.setLangpref(profile.getLangpref());
            accountVO.setFavcategory(profile.getFavcategory());
            accountVO.setMylistopt(profile.getMylistopt());
            accountVO.setBanneropt(profile.getBanneropt());
        }
        return accountVO;
    }
    private AccountSignOn accountVOToAccountSignOn(AccountVO accountVO) {
        AccountSignOn accountSignOn = new AccountSignOn();
        accountSignOn.setUsername(accountVO.getUsername());
        accountSignOn.setPassword(accountVO.getPassword());
        return accountSignOn;
    }

    private Profile accountVOToProfile(AccountVO accountVO) {
        Profile profile = new Profile();
        profile.setUsername(accountVO.getUsername());
        profile.setBanneropt(accountVO.getBanneropt());
        profile.setFavcategory(accountVO.getFavcategory());
        profile.setLangpref(accountVO.getLangpref());
        profile.setMylistopt(accountVO.getMylistopt());
        return profile;
    }

    private Account accountVOToAccount(AccountVO accountVO) {
        Account account = new Account();
        account.setUsername(accountVO.getUsername());
        account.setEmail(accountVO.getEmail());
        account.setFirstName(accountVO.getFirstName());
        account.setLastName(accountVO.getLastName());
        account.setStatus(accountVO.getStatus());
        account.setAddress1(accountVO.getAddress1());
        account.setAddress2(accountVO.getAddress2());
        account.setCity(accountVO.getCity());
        account.setState(accountVO.getState());
        account.setZip(accountVO.getZip());
        account.setCountry(accountVO.getCountry());
        account.setPhone(accountVO.getPhone());
        return account;
    }
}